package com.launch.carmanager.common.base;

import com.blankj.utilcode.util.GsonUtils;
import com.launch.carmanager.common.AppConfigInfo;
import com.launch.carmanager.common.Constants;
import com.launch.carmanager.common.utils.SignUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ganquan on 2019/3/28 10:30
 */
public abstract class BaseRequest {

    protected final ParameterBuilder paramBuilder;

    public BaseRequest() {
        paramBuilder = new ParameterBuilder();
    }

    protected final class ParameterBuilder {

        private Map<String, Object> params;
        private Map map = new HashMap();

        ParameterBuilder() {
            params = new HashMap<String, Object>();
            params.put("partner", Constants.PARTNER);
            params.put("loginUserId", Constants.USER_ID);
            params.put("clientVersion", AppConfigInfo.app_version);
        }

        public ParameterBuilder clear() {
            params.clear();
            return this;
        }

        public ParameterBuilder add(String key, Object value) {
            map.put(key, value);
            map.put("loginUserId", Constants.USER_ID);
            map.put("token", Constants.TOKEN);
            return this;
        }

        public Map<String, Object> get() {
            String data = GsonUtils.toJson(map);
            String sign = SignUtil.dataDealWith(data);
            params.put("data", data);
            params.put("sign", sign);

            return params;
        }

    }

    /**
     * 获取请求参数
     */
    public Map<String, Object> getQueryMap() {
        return paramBuilder.get();
    }
}
